package com.zhan.ielts.dao.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.ielts.dao.UsersDetailDao;
import com.zhan.ielts.domain.UsersDetail;

import java.util.Map;

@Repository
public class UsersDetailDaoImpl extends BaseDaoImpl<UsersDetail> implements
        UsersDetailDao {

    @Override
    public void saveOrUpdate(UsersDetail entity) {
        try {
            if (entity != null) {
                if (StringUtils.isBlank(entity.getId()))
                    entity.setId(generateId());
                sqlSessionTemplate.insert(getSqlName("saveOrUpdate"), entity);
            }
        } catch (Exception e) {
            throw new DaoException(String.format("添加对象出错！语句：%s",
                    getSqlName("saveOrUpdate")), e);
        }

    }

    @Override
    public void updateBySelective(UsersDetail entity) {
        try {
            if (entity != null) {
                if (StringUtils.isBlank(entity.getId()))
                    entity.setId(generateId());
                sqlSessionTemplate.update(getSqlName("updateBySelective"), entity);
            }
        } catch (Exception e) {
            throw new DaoException(String.format("修改对象出错！语句：%s",
                    getSqlName("updateBySelective")), e);
        }
    }

    @Override
    public Long selectTableName() {
        try {
            return sqlSessionTemplate.selectOne(getSqlName("selectTableName"));
        } catch (Exception e) {
            throw new DaoException(String.format("查询对象总数出错！语句：%s", getSqlName("selectTableName")), e);
        }
    }

    @Override
    public void updateColumnByUserId(Map params) {
        sqlSessionTemplate.update(getSqlName("updateColumnByUserId"),params);
    }
}
